home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / TransSkel Pascal 2.5 / TransEdit / TinyEdit / TinyEdit.p < prev    next >
Encoding:
Text File  |  1994-12-09  |  4.5 KB  |  210 lines  |  [TEXT/PJMM]

  1. {    TinyEdit - Minimal TransEdit Demonstration.}
  2.  
  3. {    The project should include TinyEdit.p (this file), TransEdit.p,}
  4. {    FakeAlert.p, TransSkel.p (or a library made from TransSkel.p)}
  5. {    Runtime.lib and Interface.lib.  Also, in the Run options dialog, set}
  6. {    the resource file to use to be TinyEdit.proj.rsrc. }
  7.  
  8. {    8 November 1986        Paul DuBois}
  9.  
  10. {    8 January 1987             Ported to LightSpeed Pascal by Owen Hartnett        }
  11. {    Ωhm Software Company, 163 Richard Drive, Tiverton, RI 02878            }
  12. {    2 December 1988 Made modifications for LSP 2.0        - OH                }
  13.  
  14. program TinyEdit;
  15.  
  16.     uses
  17. {$IFC UNDEFINED THINK_PASCAL}
  18.         Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf, 
  19. {$ENDC}
  20.         TransEdit, TransSkel;
  21.  
  22.     const
  23.         aboutAlrt = 1000;    { "About..." alert number }
  24.  
  25.         { File menu item numbers }
  26.  
  27.         new = 1;        { begin new window }
  28.         open = 2;            { open existing file }
  29.         close = 3;            { close window }
  30.         quit = 5;
  31.  
  32.             { Edit menu item numbers }
  33.  
  34.         undo = 1;
  35.         cut = 3;
  36.         copy = 4;
  37.         paste = 5;
  38.         clear = 6;
  39.  
  40.     var
  41.         lastFront, editWind: WindowPtr;    { keeps track of front window }
  42.         { non-nil if edit window open }
  43.  
  44.         fileMenu, editMenu: MenuHandle;
  45.  
  46.         dummy: boolean;                        { may be used for memory mngment }
  47. {    Set File/Edit menu items according to type of front window.}
  48.  
  49. {    The general behavior is:}
  50.  
  51. {    New and Open enabled if an edit window is not open, otherwise they}
  52. {    are disabled.}
  53.  
  54. {    Close enabled when an edit or DA window is in front (i.e.,}
  55. {    when there's a window at all).}
  56.  
  57. {    Undo disabled when the edit window is in front.}
  58.  
  59.     procedure SetMenus;
  60.  
  61.     begin
  62.         DisableItem(fileMenu, close);    { assume no window at all }
  63.         EnableItem(editMenu, undo);
  64.  
  65.         if FrontWindow <> nil then
  66.             begin
  67.                 EnableItem(fileMenu, close);
  68.                 if (IsEWindow(FrontWindow)) then    { the edit window's in front }
  69.                     DisableItem(editMenu, undo);
  70.             end;
  71.         if editWind = nil then
  72.             begin
  73.                 EnableItem(fileMenu, new);
  74.                 EnableItem(fileMenu, open);
  75.             end
  76.         else
  77.             begin
  78.                 DisableItem(fileMenu, new);
  79.                 DisableItem(fileMenu, open);
  80.             end;
  81.     end;
  82.  
  83. {    Got an activate or deactivate.  It doesn't matter which, really.}
  84. {    Set the text menus appropriately for the front window, and draw}
  85. {    the menu bar, as these menus might change state from enabled to}
  86. {    disabled or vice-versa.}
  87.  
  88.  
  89.     procedure Activate (active: Boolean);
  90.  
  91.     begin
  92.         SetMenus;
  93.     end;
  94.  
  95. {    Close selected from File menu, or close box of edit window was}
  96. {    clicked.}
  97.  
  98.     procedure myClose;
  99.  
  100.     begin
  101.         if (EWindowClose(editWind)) then
  102.             editWind := nil;
  103.         SetMenus;
  104.     end;
  105.  
  106. {    Make a new edit window.  Set the title to "Untitled" if not bound}
  107. {    to file, so that the window titling works the same whether}
  108. {    TransEdit is compiled in single or multiple window mode.}
  109.  
  110.     procedure MakeWind (bindToFile: Boolean);
  111.  
  112.         var
  113.             r: Rect;
  114.  
  115.     begin
  116.         SetRect(r, 4, 45, 504, 335);
  117.         editWind := NewEWindow(r, '', false, WindowPtr(-1), true, longint(0), bindToFile);
  118.         if editWind <> nil then
  119.             begin
  120.                 if not bindToFile then
  121.                     SetWTitle(editWind, 'Untitled');
  122.                 ShowWindow(editWind);
  123.             end;
  124.     end;
  125.  
  126. {    File menu handler}
  127.  
  128.     procedure DoFileMenu (item: integer);
  129.  
  130.         var
  131.             theWind: WindowPtr;
  132.             myPeek: WindowPeek;
  133.  
  134.     begin
  135.         theWind := FrontWindow;
  136.         case item of
  137.             new: 
  138.                 MakeWind(false);
  139.             open: 
  140.                 MakeWind(true);
  141.             close: 
  142.                 if IsEWindow(theWind) then
  143.                     myClose
  144.                 else
  145.                     begin
  146.                         myPeek := WindowPeek(theWind);
  147.                         CloseDeskAcc(myPeek^.windowKind);
  148.                     end;
  149.             quit: 
  150.                 if ClobberEWindows = true then
  151.                     SkelWhoa;
  152.             otherwise
  153.         end;
  154.         SetMenus;
  155.     end;
  156.  
  157. {    Handle selection of About… item from Apple menu}
  158.  
  159.     procedure DoAbout;
  160.  
  161.         var
  162.             ignore: integer;
  163.  
  164.     begin
  165.         ignore := Alert(aboutAlrt, nil);
  166.     end;
  167.  
  168. {    Background procedure.  Check front window, reset menus if it}
  169. {    changes.}
  170.  
  171.     procedure CheckFront;
  172.  
  173.     begin
  174.         if FrontWindow <> lastFront then
  175.             begin
  176.                 SetMenus;
  177.                 lastFront := FrontWindow;
  178.             end;
  179.     end;
  180.  
  181. begin
  182.     lastFront := nil;
  183.     editWind := nil;
  184.  
  185. {    Initialize TransSkel, create menus and install handlers.}
  186.  
  187.     SkelInit(6, nil);
  188.     TransEditInit;
  189.     SkelApple('About TinyEdit...', @DoAbout);
  190.  
  191.     fileMenu := NewMenu(1000, 'File');
  192.     AppendMenu(fileMenu, 'New/N;Open.../O;(Close/W;(-;Quit/Q');
  193.     dummy := SkelMenu(fileMenu, @DoFileMenu, nil, false);
  194.  
  195.     editMenu := NewMenu(1001, 'Edit');
  196.     AppendMenu(editMenu, 'Undo/Z;(-;Cut/X;Copy/C;Paste/V;Clear');
  197.     dummy := SkelMenu(editMenu, @EWindowEditOp, nil, true);
  198.  
  199. {    Do TransEdit-specific setup:  set creator for any files created,}
  200. {    set default event notification procedures for new windows.}
  201.  
  202.     SetEWindowProcs(nil, nil, @Activate, @myClose);
  203.  
  204. {    Process events until user quits,}
  205. {    then clean up and exit}
  206.  
  207.     SkelBackground(@CheckFront);
  208.     SkelMain;
  209.     SkelClobber;
  210. end.